﻿/********************************************************************
* Copyright (C) 2012 iTouch, Inc (http://www.itouch.com.cn) 
* All rights reserved
* Created by ZhuDongDong <dongdong_82975389@163.com> at 2012-3-30 0:34:24

* 版权所有：浙大网新易盛网络通讯有限公司
* 文件名称：JsonSerializer
* 作    者：朱冬冬(dongdong_82975389@163.com)
* 创建时间：2012-3-30 0:34:24
* 说    明：
*
********************************************************************
$Id: JsonSerializer.cs 27 2012-03-30 01:26:20Z dongdong82975389@gmail.com $
$Rev: 27 $ 
$Author: dongdong82975389@gmail.com $ 
$Date: 2012-03-30 01:26:20 +0000 (Fri, 30 Mar 2012) $
*********************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IDb;
using System.Web.Script.Serialization;

namespace Serialization
{
    /// <summary>
    /// 序列化Json数据格式类
    /// </summary>
    public class JsonSerializer : ISerializer
    {
        /// <summary>
        /// 定义一个静态只读的JavaScriptSerializer类
        /// </summary>
        private static readonly JavaScriptSerializer m_serializer;

        /// <summary>
        /// 静态构造函数，构造一个JavaScriptSerializer一个类。
        /// </summary>
        static JsonSerializer()
        {
            m_serializer = new JavaScriptSerializer();
        }
        /// <summary>
        /// 将对象序列为Json数据格式
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public string Serialize<T>(T obj)
        {
            string result = m_serializer.Serialize(obj);
            return result;
        }

        /// <summary>
        /// 将Json数据格式的字符串反序列化为一个对象
        /// </summary>
        /// <returns></returns>
        public T Deserialize<T>(string josnString)
        {
            T obj = m_serializer.Deserialize<T>(josnString);
            return obj;
        }

        /// <summary>
        /// 将对象序列为Json数据格式
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string SerializeObject<T>(T obj)
        {
            string result = m_serializer.Serialize(obj);
            return result;
        }

        /// <summary>
        /// 将Json数据格式的字符串反序列化为一个对象
        /// </summary>
        /// <returns></returns>
        public static T DeserializeObject<T>(string josnString)
        {
            T obj = m_serializer.Deserialize<T>(josnString);
            return obj;
        }
    }
}